package cn.topevery.garden.dal.logic;

import cn.hutool.core.bean.BeanUtil;
import cn.topevery.garden.dal.dao.TaskDocumentDao;
import cn.topevery.garden.dal.dao.TaskOnSiteDao;
import cn.topevery.garden.dal.dao.event.EventInfoDao;
import cn.topevery.garden.entity.po.event.EventInfo;
import cn.topevery.garden.entity.query.*;
import cn.topevery.garden.entity.vo.SpecialScoreVO;
import cn.topevery.garden.entity.vo.TaskDocumentVO;
import cn.topevery.garden.entity.vo.TaskOnSiteVO;
import cn.topevery.garden.entity.vo.event.EventInfoVO;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
public class EvaluationLogic extends BaseLogic<EventInfo> {
    @Autowired
    EventInfoDao eventInfoDao;

    @Autowired
    private TaskDocumentDao taskDocumentDao;

    @Autowired
    private TaskOnSiteDao taskOnSiteDao;

    @Autowired
    private SpecialScoreLogic specialScoreLogic;

    @Override
    public BaseEntityDao<EventInfo, Integer> getDao() {
        return eventInfoDao;
    }

    public DataPager<Map<String, Object>> getStatisticList(EvaluationQuery query) {
        return eventInfoDao.getStatisticList(query);
    }

    public JsonResult<?> getCompanyEvaluation(EvaluationQuery query) {
        if (query.getDeductType() == null) {
            return JsonResult.error("考评类型不得为空");
        }
        DataPager resultPage = new DataPager<>();
        switch (query.getDeductType()) {
            case 1: {//日常综合考评扣分记录
                EventInfoQuery eventInfoQuery = BeanUtil.copyProperties(query, EventInfoQuery.class);
                DataPager<EventInfoVO> pager = eventInfoDao.getEvtSearchPage(eventInfoQuery, null, Collections.emptyList());
                pager.records.forEach(vo -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", vo.getId());
                    map.put("checkType", "日常综合检查");
                    map.put("checkCode", vo.getCode());
                    map.put("checkTime", vo.getReportDate());
                    map.put("checker", vo.getReportName());
                    map.put("createTime", vo.getDbCreateDate());
                    map.put("deductValue", vo.getActualKfValue());
                    map.put("remark", vo.getRemark());
                    resultPage.records.add(map);
                });
                resultPage.total = pager.total;
                break;
            }
            case 2: {//内业检查扣分记录
                TaskDocumentQuery taskDocumentQuery = BeanUtil.copyProperties(query, TaskDocumentQuery.class);
                DataPager<TaskDocumentVO> pager = taskDocumentDao.listPage(taskDocumentQuery);
                pager.records.forEach(vo -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", vo.getId());
                    map.put("checkType", "内业检查");
                    map.put("checkCode", vo.getCode());
                    map.put("checkTime", vo.getEvaluationTime());
                    map.put("checker", vo.getEvaluationUserName());
                    map.put("createTime", vo.getDbCreateDate());
                    map.put("deductValue", vo.getScore());
                    map.put("remark", vo.getEvaluationContent());
                    resultPage.records.add(map);
                });
                resultPage.total = pager.total;
                break;
            }
            case 3: {//现场抽查扣分记录
                TaskOnSiteQuery taskOnSiteQuery = BeanUtil.copyProperties(query, TaskOnSiteQuery.class);
                DataPager<TaskOnSiteVO> pager = taskOnSiteDao.listPage(taskOnSiteQuery);
                pager.records.forEach(vo -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", vo.getId());
                    map.put("checkType", "现场抽查");
                    map.put("checkCode", vo.getCode());
                    map.put("checkTime", vo.getEvaluationTime());
                    map.put("checker", vo.getEvaluationUserName());
                    map.put("createTime", vo.getDbCreateDate());
                    map.put("deductValue", vo.getScore());
                    map.put("remark", vo.getEvaluationContent());
                    resultPage.records.add(map);
                });
                resultPage.total = pager.total;
                break;
            }
            case 4: {//特殊加减分扣分记录
                SpecialScoreQuery specialScoreQuery = BeanUtil.copyProperties(query, SpecialScoreQuery.class);
                DataPager<SpecialScoreVO> pager = specialScoreLogic.listPage(specialScoreQuery);
                pager.records.forEach(vo -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", vo.getId());
                    map.put("checkType", "现场抽查");
                    map.put("checkCode", vo.getCode());
                    map.put("checkTime", vo.getEvaluationTime());
                    map.put("checker", vo.getEvaluationUserName());
                    map.put("createTime", vo.getDbCreateDate());
                    map.put("deductValue", vo.getScore());
                    map.put("remark", vo.getEvaluationContent());
                    resultPage.records.add(map);
                });
                resultPage.total = pager.total;
                break;
            }
            default:
                break;
        }
        return JsonResult.ok(resultPage);
    }
}
